package com.gxa.hualianeshop.controller;

import com.gxa.hualianeshop.common.base.R;
import com.gxa.hualianeshop.common.base.ResultCode;
import com.gxa.hualianeshop.common.exception.BizException;
import com.gxa.hualianeshop.entity.pojo.Spu;
import com.gxa.hualianeshop.service.SkuService;
import com.gxa.hualianeshop.service.SpuService;
import com.gxa.hualianeshop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * @author bill
 * @date 2023/7/21 15:44
 */


@Api("商品的web接口")
@RestController
@RequestMapping("/goods")
public class GoodsController {

    //TODO 通过dubbo方式远程调用GoodsService服务
    @Reference
    private SpuService spuService;

    @Reference
    private SkuService skuService;

    @Reference
    private UserService userService;

    @ApiOperation("通过id获取spu商品信息")
    @GetMapping("/getSpuById")
    public R getSpuById(Long id){
        return R.ok(spuService.getById(id));
    }

    @ApiOperation("通过id获取sku商品信息")
    @GetMapping("/getSkuById")
    public R getSkuById(Long id){
        return R.ok(skuService.getById(id));
    }


    @ApiOperation("添加spu商品信息")
    @PostMapping("/save")
    public R save(@RequestBody Spu spu){
        Spu save = spuService.save(spu);
        if(save== null){
            throw new BizException(ResultCode.FAILED_CRUD);
        }
        return R.ok(save);
    }
}
